var health = 100;

var Enemy:GameObject;
var characterLayer:LayerMask;

var gravity = 18;

var visionAngle:float = 35;
var visionRange:float = 10;
private var distanceToPlayer:float;
private var canSeePlayer:boolean=false;

var hit:RaycastHit;

function Start()
{
	Enemy.animation.Stop();
	var idle = Enemy.animation["Idle"];
	idle.wrapMode = WrapMode.Loop;
	
	var run_fast = Enemy.animation["RunFast"];
	run_fast.wrapMode = WrapMode.Loop;
	
	idle.layer=-1;
	run_fast.layer=-1;
	
	Enemy.animation.CrossFade("Idle");
}

function Update(){
	seekSurvivor();
}

function seekSurvivor(){
	for(var survivor : GameObject in GameObject.FindGameObjectsWithTag("Survivor")){
		directionToPlayer=survivor.transform.position-transform.position;
		distanceToPlayer=Vector3.Distance(transform.position,survivor.transform.position);
		
		var playerAngle=Vector3.Angle(directionToPlayer,transform.forward);
		
		if((Mathf.Abs(playerAngle)<visionAngle && distanceToPlayer<visionRange) || canSeePlayer){
			Physics.Linecast(transform.position,survivor.transform.position,hit,~characterLayer.value);
			
			if(hit.collider.name == survivor.collider.name){
				if(distanceToPlayer > 3){
					var moveDirection=transform.forward;
					Enemy.animation.CrossFade("RunFast");
				}
				else{
					Enemy.animation.CrossFade("Idle");
				}
				var tempRot=Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(directionToPlayer),2*Time.deltaTime);
				tempRot.x=0;
				tempRot.z=0;
				transform.rotation=tempRot;
				canSeePlayer = true;
			}
			else{
				Enemy.animation.CrossFade("Idle");
			}
		}
	}
	moveDirection.y -= gravity*Time.deltaTime;
	var controller:CharacterController=GetComponent(CharacterController);
	var flags=controller.Move(moveDirection*14*Time.deltaTime);
	
}


function makeDamage(damage:float){
	health -= damage;
	
	if(health<=0){
		Destroy(gameObject);
	}
}